package com.suxin.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname IndexController
 * @Description [ TODO ]
 * @Author Tang
 * @Date 2020/12/21 15:27
 * @Created by ASUS
 */
@RestController
@RequestMapping("/redis")
public class IndexController {

    @GetMapping("/start")
    public String start() {

        return "start run...!";
    }

//    private final String prefix = "oci:product:electrolyzer:history_";
//    private final String prefix_01 = "oci:product:electrolyzer:history_2021-";
//    private final String yearPrefix = "2021-";
//
//
//    String[] having = new String[]{"04-06","04-07","04-08","04-05","04-09","04-04","04-10","04-11"};
//
//    String[] noQ = new String[]{"04-12"};
//
//    @Resource
//    RedisUtil redisUtil;
//
//    @Resource
//    RedisTemplate<String, Object> redisTemplate;
//
//    private List<Map<String, String>> getJSONList(String json) {
//        assert StringUtils.isEmpty(json);
//        JSONReader jsonReader = new JSONReader(new StringReader(json));
//        jsonReader.startArray();
//        List<Map<String, String>> rsList = new ArrayList<Map<String, String>>();
//        Map<String, String> map = null;
//        while (jsonReader.hasNext()) {
//            jsonReader.startObject();
//            map = new HashMap<>(8);
//            while (jsonReader.hasNext()) {
//                String key = jsonReader.readString();
//                String value = jsonReader.readObject().toString();
//                map.put(key, value);
//            }
//            rsList.add(map);
//            jsonReader.endObject();
//        }
//        return rsList;
//    }
//
//    public static List<String> getMonthDays(LocalDate startDate,LocalDate endDate) {
//        List<String> dates = new ArrayList<>(16);
//        while (startDate.isBefore(endDate)) {
//            dates.add(startDate.toString());
//            startDate = startDate.plusDays(1);
//        }
//        return dates;
//    }
//
//    @GetMapping("/test02")
//    public String test02() {
//
//        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
//
//        List<String> havingMonth = Arrays.stream(having)
//                .map((item) -> "2021-" + item)
//                .collect(Collectors.toList());
////        havingMonth.addAll(
////                Arrays.stream(noQ)
////                        .map((item) -> "2021-" + item)
////                        .collect(Collectors.toList())
////        );
//
//        LocalDate localDate1 = LocalDate.of(2021, 4, 9);
//        LocalDate localDate2 = LocalDate.of(2021, 4, 13);
//
//        List<String> monthDays = getMonthDays(localDate1, localDate2);
//
//        monthDays.forEach((item) -> {
//            if (havingMonth.contains(item)) { return; }
//            // 随机的key
//            String randomDate = havingMonth.get(new Random().nextInt(havingMonth.size()));
//            // 新key
//            String newKey = prefix + item;
//
//            String oldKey = prefix + randomDate;
//
////            if (redisTemplate.hasKey(newKey) == null || !redisTemplate.hasKey(newKey)) {
//
//                String json = Objects.requireNonNull(hashOperations.get(oldKey, "BJ")).toString();
//
//                List<Map<String, String>> rsList = getJSONList(json);
//
//                // 要更新成的time
//                LocalDate currentDate = LocalDate.parse(item,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//
//                rsList.forEach((map) -> {
//                    // 原先的time
//                    String time = map.get("time");
//                    // 解析旧的time
//                    LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                    LocalDateTime newDateTime = LocalDateTime.of(localDateTime.getYear(), currentDate.getMonth(), currentDate.getDayOfMonth(),
//                            localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
//                    map.put("time", newDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//                });
//
//                String uhdeJson = hashOperations.get(oldKey, "UHDE").toString();
//                List<Map<String, String>> uhdeList = getJSONList(uhdeJson);
//                uhdeList.forEach((map) -> {
//                    // 原先的time
//                    String time = map.get("time");
//                    // 解析旧的time
//                    LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                    LocalDateTime newDateTime = LocalDateTime.of(localDateTime.getYear(), currentDate.getMonth(), currentDate.getDayOfMonth(),
//                            localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
//                    map.put("time", newDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//                });
//
//                String cecJson = hashOperations.get(oldKey, "CEC").toString();
//                List<Map<String, String>> cecList = getJSONList(cecJson);
//                cecList.forEach((map) -> {
//                    // 原先的time
//                    String time = map.get("time");
//                    // 解析旧的time
//                    LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                    LocalDateTime newDateTime = LocalDateTime.of(localDateTime.getYear(), currentDate.getMonth(), currentDate.getDayOfMonth(),
//                            localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
//                    map.put("time", newDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//                });
//                redisUtil.hmSet(newKey, "BJ", rsList);
//                redisUtil.hmSet(newKey, "UHDE", uhdeList);
//                redisUtil.hmSet(newKey, "CEC", cecList);
////            }
//            System.out.println(randomDate + "--->newKey:" + newKey);
//        });
//        return "start run...!";
//    }
//
//    @GetMapping("/test01")
//    public String test01() {
//
//        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
//
//        // 3月15号数据 变 3月6号
//        for (String date : noQ) {
//            int nextInt = new Random().nextInt(having.length);
//            String newDate = prefix_01 + having[nextInt];
//            System.out.println("获取到的date为:" + newDate);
//
//            System.out.println(redisTemplate.hasKey(newDate));
//
//            String json = Objects.requireNonNull(hashOperations.get(newDate, "BJ")).toString();
//
//            List<Map<String, String>> rsList = getJSONList(json);
//
//            // 要更新成的time
//            LocalDate currentDate = LocalDate.parse(yearPrefix + date,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//
//            rsList.forEach((map) -> {
//                // 原先的time
//                String time = map.get("time");
//                // 解析旧的time
//                LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//
//                LocalDateTime newDateTime = LocalDateTime.of(localDateTime.getYear(), currentDate.getMonth(), currentDate.getDayOfMonth(),
//                        localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
//
//                map.put("time", newDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            });
//
////            String uhdeJson = redisUtil.hmGet(prefix + newDate, "UHDE");
//            String uhdeJson = hashOperations
//                    .get(newDate, "UHDE").toString();
//            List<Map<String, String>> uhdeList = getJSONList(uhdeJson);
//            uhdeList.forEach((map) -> {
//                // 原先的time
//                String time = map.get("time");
//                // 解析旧的time
//                LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                LocalDateTime newDateTime = LocalDateTime.of(localDateTime.getYear(), currentDate.getMonth(), currentDate.getDayOfMonth(),
//                        localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
//                map.put("time", newDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            });
//
////            String cecJson = redisUtil.hmGet(prefix + newDate, "CEC");
//            String cecJson = hashOperations.get(newDate, "CEC").toString();
//            List<Map<String, String>> cecList = getJSONList(cecJson);
//            cecList.forEach((map) -> {
//                // 原先的time
//                String time = map.get("time");
//                // 解析旧的time
//                LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                LocalDateTime newDateTime = LocalDateTime.of(localDateTime.getYear(), currentDate.getMonth(), currentDate.getDayOfMonth(),
//                        localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
//                map.put("time", newDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            });
//            redisUtil.hmSet(prefix_01 + date, "BJ", rsList);
//            redisUtil.hmSet(prefix_01 + date, "UHDE", uhdeList);
//            redisUtil.hmSet(prefix_01 + date, "CEC", cecList);
//        }
//        return "start run...!";
//
//    }

}